<?php
require_once 'init.inc';

$title = 'Результаты поиска';
$search = $par->get('search');
$results = array();


// search through products
$q = createQuery("
	SELECT id
	FROM cf_products 
	WHERE 
		name LIKE '%$search%' OR
		model LIKE '%$search%' OR
		short_descr LIKE '%$search%' OR
		full_descr LIKE '%$search%'
");
$q->execute();
while ($q->fetch()) 
{
	$product = new Product($q->at(0));
	$manufacturer = new Manufacturer($product->manufacturerId());
	$name = $product->name().' '.$manufacturer->name().' '.$product->model();
	$results[] = array('Товары', "product.php?productId=".$q->at(0), $name);
}
$q->close();


// search through product options
$q = createQuery("
	SELECT product_id, name
	FROM cf_product_options 
	WHERE name LIKE '%$search%' 
");
$q->execute();
while ($q->fetch()) 
{
	$product = new Product($q->at(0));
	$manufacturer = new Manufacturer($product->manufacturerId());
	$name = $product->name().' '.$manufacturer->name().' '.$product->model().' - '.$q->at(1);
	$results[] = array('Товары', "product.php?productId=".$q->at(0), $name);
}
$q->close();



// search through pages
$q = createQuery("
	SELECT id,title
	FROM cf_pages
	WHERE 
		title LIKE '%$search%' OR
		keywords LIKE '%$search%' OR
		menu_name LIKE '%$search%' OR
		txt LIKE '%$search%'
");
$q->execute();
while ($q->fetch()) 
{
	$results[] = array('Страницы', "page.php?id=".$q->at(0), $q->at(1));
}
$q->close();



// search through articles
$q = createQuery("
	SELECT id,title
	FROM articles
	WHERE 
		title LIKE '%$search%' OR
		keywords LIKE '%$search%' OR
		summary LIKE '%$search%' OR
		txt LIKE '%$search%'
");
$q->execute();
$articlesPage = new Page(ARTICLES_PAGE_ID);
while ($q->fetch()) 
{
	$results[] = array($articlesPage->title(), "article.php?articleId=".$q->at(0), $q->at(1));
}
$q->close();



// search through FAQ
$q = createQuery("
	SELECT id,question
	FROM faq
	WHERE 
		question LIKE '%$search%' OR
		answer LIKE '%$search%'
");
$q->execute();
$faqPage = new Page(FAQ_PAGE_ID);
while ($q->fetch()) 
{
	$results[] = array($faqPage->title(), "faq.php#".$q->at(0), $q->at(1));
}
$q->close();



assignTemplateVars('title','search','results');
display('search');
?>